package com.connector.server;

import org.slf4j.LoggerFactory;
import com.connector.server.jmx.RedisCustomerService;
import com.connector.server.start.HandshakeCheckTimer;
import com.connector.base.connection.impl.LocalConnectionRegistry;
import com.connector.base.connection.ConnectionRegistry;
import com.connector.server.server.ConnectorSocketIOServer;
import com.connector.server.server.WebSocketServer;
import com.connector.base.connection.session.ResuableSessionManager;
import org.slf4j.Logger;

public class InstantServer
{
    private static final Logger log;
    private ResuableSessionManager resuableSessionManager;
    private WebSocketServer webSocketServer;
    private ConnectorSocketIOServer socketIOServer;
    private final ConnectionRegistry connectionRegistry;

    public InstantServer() {
        this.resuableSessionManager = new ResuableSessionManager();
        (this.connectionRegistry = new LocalConnectionRegistry()).init();
        this.webSocketServer = new WebSocketServer(this);
        this.socketIOServer = new ConnectorSocketIOServer(this);
    }

    public void start() {
        new HandshakeCheckTimer();
        final RedisCustomerService receive = new RedisCustomerService();
        receive.start();
        InstantServer.log.info("begin init websocket server");
        this.webSocketServer.init();
        InstantServer.log.info("begin start websocket server");
        this.socketIOServer.start();
        this.webSocketServer.start();
    }

    public void stop() {
        InstantServer.log.info("begin stop websocket server");
        this.webSocketServer.stop();
        InstantServer.log.info("stop websocket server success");
        this.socketIOServer.stop();
    }

    public static void main(final String[] args) {
            new InstantServer().start();
    }

    public ResuableSessionManager getResuableSessionManager() {
        return this.resuableSessionManager;
    }

    public ConnectionRegistry getConnectionRegistry() {
        return this.connectionRegistry;
    }

    static {
        log = LoggerFactory.getLogger(InstantServer.class);
    }
}
